﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components.Dimensions;
using System.Drawing;
using ITAP.ContourBuilder.Components;
using System.Windows.Forms;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddArcRadiusDimension2 : SerializedCommand
    {
        public CommandAddArcRadiusDimension2(ContourControl control)
            : base(control)
        {
        }

        ArcRadiusDimension2 dimension = null;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            // Выбираем элемент для создания размера
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                IEnumerable<IContourComponent> allComponents = this.control.ContourContainer.GetAllInnerComponents();
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ContourElement element = null;
                foreach (IContourComponent component in allComponents)
                    if (component is ContourElement && component.CanSelectByPoint(point))
                        element = component as ContourElement;

                if (element == null || element.Height == 0)
                {
                    this.Cancel();
                    return;
                }

                foreach(IContourComponent component in element.ConnectedComponents)
                    if (component is ArcRadiusDimension2)
                    {
                        this.Cancel();
                        return;
                    }

                dimension = (ArcRadiusDimension2)DimensionFactory.CreateArcRadiusDimension2(element, GlobalData.DimensionTextFont, "R");
                element.GetRootModel().AddInnerComponent(dimension);
                this.control.UpdateModel();
                this.control.Invalidate();
                this.Exit();
                return;
            }
        }

        public override string ToString()
        {
            return "Создать размер радиуса 2";
        }
    }
}
